package GUI;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

import voting.ClientVoting;


import network.Client;
import network.Operations;

import entities.Voting;

public class ViewVotingsPanel extends JPanel implements ActionListener{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	JLabel currentLabel;
	JLabel previousLabel;
	
	JTable currentTable;
	JTable previousTable;
	
	JScrollPane currentScroll;
	JScrollPane previousScroll;
	
	JButton voteButton;
	JButton viewVotes;
	JButton createVoting;
	
	JButton viewResults;
	JButton viewVotes2;
	boolean isAdmin;


	public ViewVotingsPanel(boolean isAdmin)
	{
		this.isAdmin=isAdmin;
		
		setBounds(0,0,700,500);
		setLayout(null);	
		
		currentLabel=new JLabel("Current Votings");
		currentLabel.setBounds(20,10,100,30);
		
		previousLabel=new JLabel("Previous Votings");
		previousLabel.setBounds(20,240,100,30);
		
		voteButton=new JButton("Vote");
		voteButton.setBounds(460, 100, 80, 30);
		voteButton.addActionListener(this);
		
		viewVotes = new JButton("View Votes");
		viewVotes . setBounds(460,150,120,30);
		viewVotes . addActionListener(this);
		
		viewResults=new JButton("View Results");
		viewResults.setBounds(445, 320, 110, 30);
		viewResults.addActionListener(this);

		viewVotes2=new JButton("View Votes");
		viewVotes2.setBounds(445, 370, 110, 30);
		viewVotes2.addActionListener(this);
		
		initTables();
		currentScroll=new JScrollPane(currentTable);
		currentScroll.setBounds(20,40,400,200);
		currentScroll.setAutoscrolls(true);
		
		previousScroll=new JScrollPane(previousTable);
		previousScroll.setBounds(20,270,400,200);
		previousScroll.setAutoscrolls(true);
		
		if(isAdmin)
		{
			voteButton.setVisible(false);
			createVoting=new JButton("Create New Voting");
			createVoting.setBounds(450,40,170,30);
			createVoting.addActionListener(this);
			add(createVoting);
		}
	     
		add(currentLabel);
		add(previousLabel);
		add(currentScroll);
		add(previousScroll);
		add(voteButton);
		add(viewVotes);
		add(viewVotes2);
		add(viewResults);
		//add(b3);
		
	}
   public Voting[][] getVotings()
   {
	 
	  
	   //Client.client.receiveMessage();
	  // return null;
	   return ClientVoting.getAllVotings();
	   
   }
   public void initTables()
   {
	   
	
	   
	   Voting[][] votings=getVotings();
	   
	  
	   Object [][]previousData=new Object[1][1];
	   previousData[0][0]="No previous votings available";
	   Object[][] currentData=new Object[1][1];
	   currentData[0][0]="No current votings available";
	   
	   if(votings!=null)
	   {
		   if(votings[0].length>0)
			   previousData=new Object[votings[0].length][1];
		   if(votings[1].length>0)
			   currentData=new Object[votings[1].length][1];
		   
		   for(int i=0;i<votings[0].length;i++)
		   {
			   VotingEntry button=new VotingEntry(votings[0][i],false,"View results");
			
			   previousData[i][0]=button;
		   }
		   
		   for(int i=0;i<votings[1].length;i++)
		   {
			   VotingEntry button=new VotingEntry(votings[1][i],true,"Vote");
			  
			   currentData[i][0]=button;
		   }
	   }
	  
	   currentTable=new JTable(new MyTableModel(currentData,new String[]{""}));
	   previousTable=new JTable(new MyTableModel(previousData,new String[]{""}));
	   
	   currentTable.setSelectionMode(0);
	   previousTable.setSelectionMode(0);
	
	   
	   
	   
   }
   @Override
	public void actionPerformed(ActionEvent ev) {
	   try
	   {
		if(ev.getSource()==viewResults)
		{
			VotingEntry entry=(VotingEntry)previousTable.getModel().getValueAt(previousTable.getSelectedRow(), previousTable.getSelectedColumn());
			MainFrame.getMainFrame().remove(this);
			MainFrame.getMainFrame().addVotingResultsPnl(entry.voting,isAdmin);
		}
		if(ev.getSource()==createVoting)
		{
			MainFrame.getMainFrame().remove(this);
			MainFrame.getMainFrame().addCreateVotingPnl();
		}
		if(ev.getSource() == voteButton)
		{
			VotingEntry entry=(VotingEntry)currentTable.getModel().getValueAt(currentTable.getSelectedRow(), currentTable.getSelectedColumn());
			MainFrame.getMainFrame().remove(this);
			MainFrame.getMainFrame().addVotingPanel(entry.voting);
		}
		if(ev.getSource() == viewVotes)
		{
			VotingEntry entry=(VotingEntry)currentTable.getModel().getValueAt(currentTable.getSelectedRow(), currentTable.getSelectedColumn());
			MainFrame.getMainFrame().remove(this);
			MainFrame.getMainFrame().addViewVotesPanel(entry.voting,isAdmin);
		}
		if(ev.getSource() == viewVotes2)
		{
			VotingEntry entry=(VotingEntry)previousTable.getModel().getValueAt(previousTable.getSelectedRow(), previousTable.getSelectedColumn());
			MainFrame.getMainFrame().remove(this);
			MainFrame.getMainFrame().addViewVotesPanel(entry.voting,isAdmin);
		}
	   }
	   catch(Exception e)
	   {
		   
	   }
	}
	public static void main(String[]args)
	{
		/*JFrame frame=new JFrame();
		frame.setBounds(0,0,750,550);
		frame.setContentPane(new ViewVotingsPanel(frame));
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);*/
		
		
	}
	
	

}
class MyTableModel extends AbstractTableModel {
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String[] columnNames ;
    private Object[][] data ;

    public MyTableModel(Object[][]data,String[] columnNames)
    {
    	this.data=data;
    	this.columnNames=columnNames;
    }
    public int getColumnCount() {
        return columnNames.length;
    }

    public int getRowCount() {
        return data.length;
    }

    public String getColumnName(int col) {
        return columnNames[col];
    }

    public Object getValueAt(int row, int col) {
        return data[row][col];
    }

    public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }

    /*
     * Don't need to implement this method unless your table's
     * editable.
     */
    public boolean isCellEditable(int row, int col) {
        //Note that the data/cell address is constant,
        //no matter where the cell appears onscreen.
      return false;
    }

    /*
     * Don't need to implement this method unless your table's
     * data can change.
     */
    public void setValueAt(Object value, int row, int col) {
        data[row][col] = value;
        fireTableCellUpdated(row, col);
    }
   
}
class VotingEntry
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	Voting voting;
	boolean isNew;
	public boolean isNew() {
		return isNew;
	}
	public void setNew(boolean isNew) {
		this.isNew = isNew;
	}
	public VotingEntry(Voting voting,boolean isNew,String text)
	{
		this.voting=voting;
		this.isNew=isNew;
		
	}
	public String toString()
	{
	 return voting.getQuestions();
	}
}
